﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public class TabFormCollection : List<System.Windows.Forms.Form>
    {
        private TabFormPanel TabFormPanel;

        public TabFormCollection(TabFormPanel TabFormPanel)
        {
            this.TabFormPanel = TabFormPanel;
        }

        public System.Windows.Forms.Form Add<TForm>() where TForm : System.Windows.Forms.Form, new()
        {
            var item = new TForm();
            this.Add(item);
            return item;
        }

        public new void Add(System.Windows.Forms.Form Item)
        {
            try
            {
                WindowsAPI.SuspendDrawing(this.TabFormPanel);
                Item.TopLevel = false;
                Item.Parent = this.TabFormPanel.Panel;
                Item.Dock = DockStyle.Fill;
                Item.FormBorderStyle = FormBorderStyle.None;
                Item.FormClosing += this.Form_FormClosing;
                Item.Show();
                this.TabFormPanel.Panel.Controls.Add(Item);
                this.TabFormPanel.Rectangles.Add(Item, default);
                base.Add(Item);
                this.TabFormPanel.SelectedForm = Item;
                this.TabFormPanel.Invalidate(this.TabFormPanel.Rectangle);
                WindowsAPI.ResumeDrawing(this.TabFormPanel);
            }
            finally
            {
                WindowsAPI.ResumeDrawing(this.TabFormPanel);
            }
        }

        public new void Clear()
        {
            for (int i = 0, loopTo = this.TabFormPanel.Forms.Count - 1; i <= loopTo; i++)
                this.TabFormPanel.Forms[0].Close();
        }

        public void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                WindowsAPI.LockWindowUpdate((int)this.TabFormPanel.Handle);
                int @int = -1;
                for (int i = 0, loopTo = this.TabFormPanel.Forms.Count - 1; i <= loopTo; i++)
                {
                    if (ReferenceEquals(this.TabFormPanel.Forms[i], sender))
                    {
                        @int = i;
                        break;
                    }
                }

                if (@int > -1 && sender is System.Windows.Forms.Form form && form.IsDisposed == false)
                {
                    if (this.TabFormPanel.Forms.Count - 1 > @int)
                    {
                        this.TabFormPanel.SelectedForm = this.TabFormPanel.Forms[@int + 1];
                    }
                    else if (this.TabFormPanel.Forms.Count > 1)
                    {
                        this.TabFormPanel.SelectedForm = this.TabFormPanel.Forms[@int - 1];
                    }

                    this.TabFormPanel.Rectangles.Remove((System.Windows.Forms.Form)sender);
                    Remove(form);
                    this.TabFormPanel.Invalidate(this.TabFormPanel.Rectangle);
                }
            }
            finally
            {
                WindowsAPI.LockWindowUpdate((int)IntPtr.Zero);
            }
        }
    }
}